﻿using Dapper;
using duia.Canteenpay.Infrastructure;
using duia.Canteenpay.ViewModel;
using duia.Canteenpay.ViewModel.Users;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace duia.Canteenpay.Application.Querys
{
    /// <summary>
    /// 用户查询
    /// </summary>
    public class UserQuerys : AppDapper
    {

        public UserQuerys(string connectionString) : base(connectionString)
        {
        }

        /// <summary>
        /// 根据Id获取用户信息
        /// </summary>
        /// <param name="keyId"></param>
        /// <returns></returns>
        public async Task<UserModelResponse> Find(Guid keyId)
        {
            return await base.QueryFirstOrDefaultAsync<UserModelResponse>(" select * from canpay_users where keyid=@keyid", new { keyid = keyId });
        }


        /// <summary>
        /// 分页查询系统用户
        /// </summary>
        /// <param name="parame"></param>
        /// <returns></returns>
        public async Task<BasePageResponse<UserModelResponse>> FindPageUser(SearchPageUserRequest parame)
        {
            BasePageResponse<UserModelResponse> basePageResponse = new BasePageResponse<UserModelResponse>();

            StringBuilder sql = new StringBuilder(" select * from canpay_users ");

            StringBuilder where = new StringBuilder("where 1=1 ");

            var mySqlParameter = new DynamicParameters();

            if (!string.IsNullOrWhiteSpace(parame.Fliter.UserName))
            {
                where.Append(" and UserName like '%@UserName%'");
                mySqlParameter.Add("@UserName", parame.Fliter.UserName);
            }

            if (string.IsNullOrWhiteSpace(parame.Sort.SortPropertyName))
                parame.Sort.SortPropertyName = BaseSortCriteriaRequest.CREATE_TIME;

            var valueTuple = base.QueryPage<UserModelResponse>(sql.Append(@where).ToString(), mySqlParameter, parame.Sort.SortPropertyName, parame.Sort.Ascending, parame.PageIndex, parame.PageSize);

            basePageResponse.Count = valueTuple.Item1;
            basePageResponse.Data = valueTuple.Item2.ToList();

            return basePageResponse;
        }


    }
}
